package com.wang.myblog.controller;

import com.sun.javafx.collections.MappingChange;
import com.wang.myblog.entity.*;
import com.wang.myblog.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class SystemController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private TagService tagService;

    @Autowired
    private KindService kindService;

    @Autowired
    private AdminService adminService;

    @Autowired
    private FrontService frontService;

    @Autowired
    private LinkService linkService;

    @Autowired
    private StatisticsService statisticsService;

    @Autowired
    private CommentService commentService;


    /**
     * 前往前台首页
     */
    @GetMapping({"/", "/index"})
    public String index(Map<String, Object> map, Integer page) {
        Admin user = adminService.queryById(1);
        user.setPassword(null);
        // 获取已发布的文章数量
        Integer totalCount = articleService.getCountByStatus(1, null);
        Integer totalPage = (int) Math.ceil(totalCount / 10 + 0.5);
        // 处理page参数
        if (ObjectUtils.isEmpty(page)) {
            page = 1;
        } else if (page < 1 || page > totalPage) {
            return "front/index";
        }
        Front front = frontService.queryById(1);

        // 获取主页的10篇文章
        List<Article> articles = articleService.queryPublishedArticleOrderByPublishTime(page);
        // 获取所有标签
        List<Tag> tags = tagService.queryAll(1);
//        tags.forEach(tag -> {
//            System.out.println(tag);
//        });
        // 获取所有的文集
        List<Kind> kinds = kindService.queryAll(1);
        // 获取阅读量最高的10篇文章
        List<Article> famousArticles = articleService.queryOrderByReadCount();
        //获取日期归档
        List<Archive> archives = articleService.getArchive();
        //获取友情链接
        List<Link> links = linkService.queryAll();

        map.put("articles", articles);
        map.put("user", user);
        map.put("front", front);
        map.put("famousArticles", famousArticles);
        map.put("tags", tags);
        map.put("kinds", kinds);
        map.put("page", page);
        map.put("totalPage", totalPage);
        map.put("archives", archives);
        map.put("links", links);
        statisticsService.add(new Date());
        return "front/index";
    }

    /**
     * 搜索
     */
    @GetMapping("/search")
    public String search(Map<String,Object> map,String msg,Integer kindId,Integer tagId,String date,Integer page){
        Admin user = adminService.queryById(1);
        user.setPassword(null);
        map.put("user", user);
        if (ObjectUtils.isEmpty(page)){
            page=1;
        }
        Integer totalCount = 0;

       List<Article> articles=null;
        if (!ObjectUtils.isEmpty(msg)){
            articles=articleService.queryPublishedArticleByMsg(msg,page);
            totalCount=articleService.getPublishedCountByMsg(msg);
            map.put("msg",msg);
        }else if (!ObjectUtils.isEmpty(tagId)){
            articles = articleService.queryPublishedArticleByTagId(tagId,page);
            map.put("tag",tagService.queryById(tagId));
            totalCount=articleService.getPublishedArticleCountByTagId(tagId);
            //map.put("articleCount",articleService.getPublishedArticleCountByTagId(tagId));
        }else if (!ObjectUtils.isEmpty(kindId)){
            articles = articleService.queryPublishedArticleByKindId(kindId,page);
            totalCount=articleService.getPublishedArticleCountByKindId(kindId);
            map.put("kind",kindService.queryById(kindId));
            //map.put("articleCount",articleService.getPublishedArticleCountByKindId(kindId));
        }else if (!ObjectUtils.isEmpty(date)){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年 - MM月");
            try {
                Date parse = sdf.parse(date);
                articles = articleService.queryByDate(parse,page);
                map.put("date",date);
                totalCount=articleService.getPublishedArticleCountByDate(date);
//                map.put("articleCount",articleService.getPublishedArticleCountByDate(date));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        Integer totalPage = (int) Math.ceil(totalCount / 10 + 0.5);
        map.put("articles",articles);
        map.put("page", page);
        map.put("totalPage", totalPage);
        map.put("articleCount",totalCount);
        map.put("date",date);
        return "front/search";

    }

    /**
     * 前往后台仪表盘界面
     */
    @GetMapping("/dashboard")
    public String dashboard(Map<String,Object> map){
        // 获取各个状态下文章的数量
        Integer published=articleService.getCountByStatus(1,null);
        Integer draft=articleService.getCountByStatus(0,null);
        Integer recycle=articleService.getCountByStatus(2,null);
        Integer total=published+draft+recycle;
        //文章数
        map.put("articleCount",total);
        map.put("publishCount",published);
        map.put("draft",draft);
        map.put("recycle",recycle);
        // 获取评论数
        map.put("commentCount",commentService.getCountByType(0,null));
        // 获取分类数
        map.put("kindCount",kindService.getCount());
        // 获取标签数
        map.put("tagCount",tagService.getCount());
        // 获取未读评论数
        map.put("unRead",commentService.getUnReadCount());
        map.put("statisticsDateList",statisticsService.getRecentRequestsDate());
        map.put("statisticsCountList",statisticsService.getRecentRequestsCounts());
        return "admin/dashboard";
    }

    /**
     * 前往后台-统计页面
     */
    @GetMapping("/statistics")
    public String statistics(Map<String,Object> map){
        List<Statistics> statisticsList=statisticsService.getRecentRequests();
        map.put("statisticsDateList",statisticsService.getRecentRequestsDate());
        map.put("statisticsCountList",statisticsService.getRecentRequestsCounts());
        map.put("map",statisticsService.getRequestCount());
        //分类
        map.put("kindCount",kindService.getCount());
        map.put("kindNameList",kindService.getKindNameList());
        map.put("kindArticleReadCount",kindService.getKindArticleReadCount());
        //标签
        map.put("tagCount",tagService.getCount());
        map.put("tagNameList",tagService.getTagNameList());
        map.put("tagArticleReadCount",tagService.getTagArticleReadCount());
        return "admin/statistics";
    }
}
